package com.nhjf.mobile.controller.admin.userinformation;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.RespectBinding;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.ReviewPainting;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.reviewPainting.reviewPaintingResultDto;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.ReviewPaintingQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.ReviewPaintingService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@RequestMapping("/uinformationAdmin")
@Controller
public class UserinformationAdminController{

	@Autowired
	private StudentService studentService;
	@Autowired
	private TeachersService teachersService;
	@Autowired
	private ReviewPaintingService reviewPaintingService;
	@Autowired
	private UserService userService;
	@Autowired
	private AdminSessionService adminSessionService;
	//我的改画
	@RequestMapping("/tz_pingshen")
	@ResponseBody
	public String pingshen(HttpServletRequest request, HttpServletResponse response, Model model,@RequestParam(value = "page", defaultValue = "1")Integer page,String typeOne,String status,String isOrganizationPainting,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					System.out.println(user.getUserName());
					//老师素描/速写.../
					TeachersQo teachersQo=new TeachersQo();
					teachersQo.setUserId(user.getId());
					Teacher teacher= teachersService.queryUnique(teachersQo);
					SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					if(teacher!=null) {
						ReviewPaintingQo reviewPaintingQo=new ReviewPaintingQo();
						if(StringUtils.isNotBlank(typeOne)) {
							reviewPaintingQo.setTypeOne(Long.parseLong(typeOne));
						}
						reviewPaintingQo.setSeachTeacher(true);
						reviewPaintingQo.setTeacherId(teacher.getId());
						reviewPaintingQo.setSeachUser(true);
						if(StringUtils.isNotBlank(status)) {
							reviewPaintingQo.setStatus(Integer.parseInt(status));
						}
						if(StringUtils.isNotBlank(isOrganizationPainting)) {
							reviewPaintingQo.setIsOrganizationPainting(Integer.parseInt(isOrganizationPainting));
						}
						//						reviewPaintingQo.setTypeOne(2L);
						resMap.put("CoursesQo",reviewPaintingQo);
						Pagination pagination = new Pagination();
						pagination.setPageSize(8);
						pagination.setPageNo(page);
						pagination.setCondition(reviewPaintingQo);
						pagination = reviewPaintingService.queryPagination(pagination);
						List<ReviewPainting> reviewPaintingList=(List<ReviewPainting>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(ReviewPainting reviewPainting:reviewPaintingList) {
							reviewPainting.setReleasedate(format1.format(reviewPainting.getReleasetime()));
							if(StringUtils.isNotEmpty(reviewPainting.getChangepaintingURL())){
								reviewPainting.setChangepaintingURL(imagePath+"/reviewPainting/"+reviewPainting.getChangepaintingURL());
							}
							StudentQo qo=new StudentQo();
							qo.setSeachStudent(true);
							qo.setUserid(reviewPainting.getUser().getId());
							Student student= studentService.queryUnique(qo);
							reviewPainting.setNickName(student.getNickname());
							if(!StringUtils.isEmpty(student.getAvatar())) {
								if(student.getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									student.setAvatar(imagePath+"/picture/"+student.getAvatar());
									reviewPainting.setAvatar(student.getAvatar());
								}
							}
							if(StringUtils.isNotEmpty(reviewPainting.getNotchangedURL())){
								reviewPainting.setNotchangedURL(imagePath+"/reviewPainting/"+reviewPainting.getNotchangedURL());
							}
							UserQo userQo1=new UserQo();
							userQo1.setUserId(reviewPainting.getUser().getId());
							User user1=userService.queryUnique(userQo1);
							reviewPainting.getUser().setId(user1.getId());
						}
						resMap.put("message",pagination);
						resMap.put("code", "200");
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "500");
						resMap.put("message", "没有找到老师!");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					resMap.put("code", "406");
					resMap.put("message", "请先登录!");
					return net.sf.json.JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "异常抛出！");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 根据改画id查看详情
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/reviewDetails")
	public String reviewDetails(String id,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession!=null) {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user!=null) {
					if(user.getIsRole()==4) {
						resMap.put("code", "500");
						resMap.put("message", "最高管理员不能操作!");
						return JSONObject.fromObject(resMap).toString();
					}
					ReviewPaintingQo paintingQo=new ReviewPaintingQo();
					paintingQo.setReviewId(Long.parseLong(id));
					paintingQo.setSeachUser(true);
					paintingQo.setSeachTeacher(true);
					ReviewPainting painting= reviewPaintingService.queryUnique(paintingQo);
					if(painting.getUser().getIsRole()==2) {
						StudentQo qo=new StudentQo();
						qo.setSeachStudent(true);
						qo.setUserid(painting.getUser().getId());
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						Student student= studentService.queryUnique(qo);
						student.setAvatar(imagePath+"/picture/"+student.getAvatar());
						painting.setAvatar(student.getAvatar());
						painting.setNotchangedURL(imagePath+"/reviewPainting/"+painting.getNotchangedURL());
						if(StringUtils.isNotBlank(painting.getVoiceReview())) {
							painting.setVoiceReview(imagePath+"/voice/"+painting.getVoiceReview());
						}
						painting.setChangepaintingURL(imagePath+"/reviewPainting/"+painting.getChangepaintingURL());
						painting.setNickName(student.getNickname());
						resMap.put("code", "200");
						resMap.put("message", painting);
						return JSONObject.fromObject(resMap).toString();
					}else {
						TeachersQo qo=new TeachersQo();
						qo.setSeachOrganization(true);
						qo.setSeachUser(true);
						qo.setUserId(painting.getUser().getId());
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						Teacher teacher= teachersService.queryUnique(qo);
						teacher.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
						painting.setAvatar(teacher.getAvatar());
						if(StringUtils.isNotBlank(painting.getVoiceReview())) {
							painting.setVoiceReview(imagePath+"/voice/"+painting.getVoiceReview());
						}
						painting.setNotchangedURL(imagePath+"/reviewPainting/"+painting.getNotchangedURL());
						painting.setChangepaintingURL(imagePath+"/reviewPainting/"+painting.getChangepaintingURL());
						painting.setNickName(teacher.getNickname());
						resMap.put("code", "200");
						resMap.put("message", painting);
						return JSONObject.fromObject(resMap).toString();
					}

				}else {
					resMap.put("code", "406");
					resMap.put("message", "请先登录!");
					return net.sf.json.JSONObject.fromObject(resMap).toString();
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "操作异常!");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "异常抛出!");
			return JSONObject.fromObject(resMap).toString();
		}

	}

}
